package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TProduct;
import interfaces.IProductService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class ProductAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IProductService productService;

	private TProduct productBean;
	private List<TProduct> productList;

	private String pctNo;
	private String pctType;
	private String pctName;
	private Double pctPrice;
	private String pctState;
	private Date pctDate;
	private Date pctDateTop;
	private Date pctDateEnd;
	private String pctRemark;

	// 展示对象
	public String show() throws Exception {
		if (!productService.exist(pctNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			productBean = productService.fetch(pctNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!productService.exist(pctNo)) {
			productBean = new TProduct();
			productBean.setPctNo(StringUtil.guid());
			productBean.setPctState("1");
		} else {
			productBean = productService.fetch(pctNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!productService.exist(pctNo)) {
			productBean = new TProduct();
		} else {
			productBean = productService.fetch(pctNo);
		}
		productBean.setPctNo(pctNo);
		productBean.setPctType(pctType);
		productBean.setPctName(pctName);
		productBean.setPctPrice(pctPrice);
		productBean.setPctState(pctState);
		productBean.setPctDate(new Date());
		productBean.setPctRemark(pctRemark);

		productService.persist(productBean);
		return SUCCESS;
	}
	
	// 持久化对象
	public String move() throws Exception {
		productService.remove(pctNo);
		return SUCCESS;
	}
	

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pctType", pctType);
		paramMap.put("pctName", pctName);
		paramMap.put("pctState", pctState);
		paramMap.put("pctDate", ParameterUtil.dateCombin(pctDateTop, pctDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "pctDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TProduct> resultSet = productService.search(querySet);
		productList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pctType", pctType);
		paramMap.put("pctName", pctName);
		paramMap.put("pctState", pctState);
		paramMap.put("pctDate", ParameterUtil.dateCombin(pctDateTop, pctDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "pctDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TProduct> resultSet = productService.search(querySet);
		productList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("所属品牌");
		fieldHead.add("产品名称");
		fieldHead.add("单价");
		fieldHead.add("是否可用");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TProduct productBean : productList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(productBean.getPctNo());
			fieldItem.add(productBean.getPctType());
			fieldItem.add(productBean.getPctName());
			fieldItem.add(productBean.getPctPrice().toString());
			fieldItem.add((productBean.getPctState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(productBean.getPctDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(productBean.getPctDate(), DateUtil.TIME));
			fieldItem.add(productBean.getPctRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public TProduct getProductBean() {
		return productBean;
	}

	public void setProductBean(TProduct productBean) {
		this.productBean = productBean;
	}

	public List<TProduct> getProductList() {
		return productList;
	}

	public void setProductList(List<TProduct> productList) {
		this.productList = productList;
	}

	public String getPctNo() {
		return pctNo;
	}

	public void setPctNo(String pctNo) {
		this.pctNo = pctNo;
	}

	public String getPctType() {
		return pctType;
	}

	public void setPctType(String pctType) {
		this.pctType = pctType;
	}

	public String getPctName() {
		return pctName;
	}

	public void setPctName(String pctName) {
		this.pctName = pctName;
	}

	public Double getPctPrice() {
		return pctPrice;
	}

	public void setPctPrice(Double pctPrice) {
		this.pctPrice = pctPrice;
	}

	public String getPctState() {
		return pctState;
	}

	public void setPctState(String pctState) {
		this.pctState = pctState;
	}

	public Date getPctDate() {
		return pctDate;
	}

	public void setPctDate(Date pctDate) {
		this.pctDate = pctDate;
	}

	public Date getPctDateTop() {
		return pctDateTop;
	}

	public void setPctDateTop(Date pctDateTop) {
		this.pctDateTop = pctDateTop;
	}

	public Date getPctDateEnd() {
		return pctDateEnd;
	}

	public void setPctDateEnd(Date pctDateEnd) {
		this.pctDateEnd = pctDateEnd;
	}

	public String getPctRemark() {
		return pctRemark;
	}

	public void setPctRemark(String pctRemark) {
		this.pctRemark = pctRemark;
	}

}
